package com.learning.dubbo.service.context.consumer;

import com.learning.dubbo.service.context.api.GreetingsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * ClassName: Consumer
 * Description: 消费客户端
 * Date: 2020/6/15 13:43 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Slf4j
public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();

        // 获取远程服务代理
        GreetingsService greetingsService = (GreetingsService) context.getBean("greetingsService");
        // 执行远程方法
        String result = greetingsService.sayHi("Dubbo");
        // 显示调用结果
        log.info("~~~~~~~~~~~ {} ~~~~~~~~~~~~~", result);
        // 代理对象
        log.info(greetingsService.getClass().getName());

        RpcContext rpcContext = RpcContext.getContext();

        // 本端是否为消费端，这里会返回true
//        boolean isConsumerSide = rpcContext.isConsumerSide();
//        System.out.println(isConsumerSide);
        // 获取最后一次调用的提供方IP地址
        String serverIP = rpcContext.getRemoteHost();

        System.out.println(serverIP);
        // 获取当前服务配置信息，所有配置信息都将转换为URL的参数
//        String application = rpcContext.getUrl().getParameter("application");
//        System.out.println(application);


        // 隐式参数：服务消费方端设置隐式参数
        rpcContext.setAttachment("name","sam");

        context.close();

    }
}
